package onlinebanking.server.gui;

import java.awt.Color;
import java.awt.event.WindowEvent;
import java.util.Observable;
import java.util.Observer;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

import onlinebanking.server.DataObject;
import onlinebanking.server.SimpleServer;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class AdministratorFrame extends javax.swing.JFrame implements Observer{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private TitlePanel titlePanel;
	private BodyPanel bodyPanel;
	private BottomPanel bottomPanel;
	private JButton serverButton;
	private JButton bankAccountButton;
	private JButton personalAccountButton;
	private JLabel consoleLabel;
	private JTextArea consoleArea;
	private UpperTabsPanel UpperTabsPanel;
	private LeftEarPanel leftEarPanel;

	private SimpleServer server;
	
	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args) {

		try {
			javax.swing.UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (Exception e) {
			e.printStackTrace();
		}

		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				AdministratorFrame inst = new AdministratorFrame();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	public AdministratorFrame() {
		super();
		initGUI();
	}

	public void processWindowEvent(WindowEvent w) {
		if (w.getID() == WindowEvent.WINDOW_CLOSING) {
			int decision = JOptionPane.showConfirmDialog(this,
					"Are you sure you want to close this window?",
					"Confirm Close", JOptionPane.YES_NO_OPTION,
					JOptionPane.QUESTION_MESSAGE);
			if (decision == JOptionPane.YES_OPTION) {
				dispose();
			}
		} else {
			super.processWindowEvent(w);
		}
	}

	private void initGUI() {
		try {
			getContentPane().setLayout(null);
			getContentPane().setBackground(new Color(0 , 150, 0));
			this.setResizable(false);
			this.setTitle("PDFBank Administrator's Panel");
			this.setIconImage(ImageIO.read(this.getClass().getClassLoader().getResource("images/ProgramIcon.PNG")));
			{
				titlePanel = new TitlePanel();
				getContentPane().add(titlePanel);
			}
			{
				leftEarPanel = new LeftEarPanel();
				getContentPane().add(leftEarPanel);
				leftEarPanel.setLayout(null);
				leftEarPanel.setBounds(12, 133, 173, 381);
				{
					serverButton = new JButton();
					leftEarPanel.add(serverButton);
					serverButton.setText("Server");
					serverButton.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/server.png")));
					serverButton.setBounds(0, 33, 173, 123);
					serverButton.setVerticalTextPosition(SwingConstants.BOTTOM);
					serverButton.setHorizontalTextPosition(SwingConstants.CENTER);
				}
				{
					bankAccountButton = new JButton();
					leftEarPanel.add(bankAccountButton);
					bankAccountButton.setText("Bank \r\nAccounts");
					bankAccountButton.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/bank.png")));
					bankAccountButton.setBounds(0, 156, 173, 106);
					bankAccountButton.setHorizontalTextPosition(SwingConstants.CENTER);
					bankAccountButton.setVerticalTextPosition(SwingConstants.BOTTOM);
				}
				{
					personalAccountButton = new JButton();
					leftEarPanel.add(personalAccountButton);
					personalAccountButton.setText("Personal Accounts");
					personalAccountButton.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/user.png")));
					personalAccountButton.setBounds(0, 262, 173, 119);
					personalAccountButton.setVerticalTextPosition(SwingConstants.BOTTOM);
					personalAccountButton.setHorizontalTextPosition(SwingConstants.CENTER);
				}
			}
			{
				bodyPanel = new BodyPanel();
				getContentPane().add(bodyPanel);
				bodyPanel.setLayout(null);
				{
					consoleArea = new JTextArea();
					bodyPanel.add(consoleArea);
					consoleArea.setBounds(10, 72, 564, 298);
				}
				{
					consoleLabel = new JLabel();
					bodyPanel.add(consoleLabel);
					consoleLabel.setText("Console");
					consoleLabel.setBounds(10, 47, 38, 14);
				}
			}
			{
				bottomPanel = new BottomPanel();
				getContentPane().add(bottomPanel);
				bottomPanel.setBounds(10, 520, 765, 31);
			}
			{
				UpperTabsPanel = new UpperTabsPanel();
				getContentPane().add(UpperTabsPanel);
				UpperTabsPanel.setBounds(12, 11, 763, 19);
			}
			pack();
			this.setSize(800, 600);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	
	public void bind(SimpleServer server) {
		this.server = server;
		this.server.addObserver(AdministratorFrame.this);
	}
	
	private void initalizeModule() {
		
	}
	
	private void initializeSubModule() {
		
	}

	@Override
	public void update(Observable o, Object arg) {
		// TODO Auto-generated method stub
		
//		debug message
//		System.out.println("update admin");
//		if (o instanceof SimpleServer) {
//			System.out.println("equal? " + o.equals(this.server));
//		}
//		System.out.println("data object? " + (arg instanceof DataObject));
		if (arg instanceof DataObject) {
			DataObject dataObject = (DataObject)arg;
			String recentMessage = dataObject.getRecentMessage();
			String[] bankAccounts = dataObject.getBankAccounts();
			String[] personalAccounts = dataObject.getPersonalAccounts();
//			this.setTitle(recentMessage);
			this.consoleArea.append(recentMessage + "\n");
		}
	}
}
